package o2o.web.actions.sys;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYImageUtils;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.user.UserInfo;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import o2o.system.data.SYS_CONFIG_PATH;

//图片快速处理类
@Controller
public class PictureProcessAction {

    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/config/img/process")
    @ResponseBody
    public String imgProcess(String token, Integer type, Integer imgWidth, Integer imgLength) {
        //登录验证
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        //权限验证
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }
        int count = 0;
        try {
            System.out.println("管理员开始压缩图片" + DateUtils.getCurrentDateTime());
            if (type == null) {
                JYImageUtils.AllDirFileCompress(imgWidth, imgLength);
            } else {
                switch (type) {
                    case 1:
                        //USER_IMAGE 用户图片
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.USER_IMAGE, 1, imgWidth, imgLength);
//                        count = JYImageUtils.DirCompress("F:\\aa",56,null,null);
                        break;
                    case 2:
                        //ADVERT_IMAGE;//广告图片
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.ADVERT_IMAGE, 2, imgWidth, imgLength);
                        break;
                    case 3:
                        //SHOP_IMAGE;//店铺图片
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.SHOP_IMAGE, 3, imgWidth, imgLength);
                        break;
                    case 4:
                        //GOODS_IMAGE;//商品图片
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.GOODS_IMAGE, 4, imgWidth, imgLength);
                        break;
                    case 5:
                        //ORDER_IMAGE;//订单图片
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.ORDER_IMAGE, 5, imgWidth, imgLength);
                        break;
                    case 6:
                        //NOTICE_IMAGE;//消息图片路径
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.NOTICE_IMAGE, 6, imgWidth, imgLength);
                        break;
                    case 7:
                        //USER_SIGN // 用户签到上传图片路径
                        count = JYImageUtils.DirCompress(SYS_CONFIG_PATH.IMAGE_BASE_FILE + SYS_CONFIG_PATH.USER_SIGN, 7, imgWidth, imgLength);
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("出错了，赶快去还原图片吧，请勿再次操作哦");
        }
        System.out.println("管理员压缩图片结束" + DateUtils.getCurrentDateTime());
        return "{\"status\":1,\"info\":{\"count\":" + count + "}}";
    }
}
